package com.test.thrift.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;

import javax.sql.DataSource;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by MAC on 2018/10/23.
 */
public class DBUtil {
    private static DruidDataSource druidDataSource = null;
    private static DruidPooledConnection connection = null;

    public static DataSource getDataSource(String url, String username, String password) {
        druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);

        druidDataSource.setConnectionProperties(""
                + "druid.stat.mergeSql=true;"
                + "druid.stat.slowSqlMillis=1;"
                + "druid.stat.logSlowSql=true");

        druidDataSource.setInitialSize(5);
        druidDataSource.setMinIdle(1);
        druidDataSource.setMaxActive(10);
        // 启用监控统计功能 druidDataSource.setFilters("stat");// for mysql  
        druidDataSource.setPoolPreparedStatements(false);
        return  druidDataSource;
    }
    /**
     * 获取Connection 连接
     *
     * @param url
     * @param username
     * @param password
     * @return
     */
    public static DruidPooledConnection getConnection(String url, String username, String password) {
        try {
            druidDataSource = new DruidDataSource();
            druidDataSource.setUrl(url);
            druidDataSource.setUsername(username);
            druidDataSource.setPassword(password);

            druidDataSource.setConnectionProperties(""
                    + "druid.stat.mergeSql=true;"
                    + "druid.stat.slowSqlMillis=1;"
                    + "druid.stat.logSlowSql=true");

            druidDataSource.setInitialSize(5);
            druidDataSource.setMinIdle(1);
            druidDataSource.setMaxActive(10);
            // 启用监控统计功能 druidDataSource.setFilters("stat");// for mysql  
            druidDataSource.setPoolPreparedStatements(false);
            connection = druidDataSource.getConnection();
            return connection;
        } catch (SQLException e) {
            e.printStackTrace();
            if (druidDataSource != null)
                druidDataSource.close();
            if (connection != null)
                try {
                    connection.close();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
        }
        return null;
    }
    public static void executeQuery(String url, String username, String password,String sql){
        DruidPooledConnection connection = DBUtil.getConnection( url, username, password);
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
